package com.intellagric.controller.module.agriecom_news_type;

import com.intellagric.agriecom.module.agriecom_news_type.NewsTypeService;
import com.intellagric.common.pojo.LayuiDataGridResult;
import com.intellagric.common.pojo.ResponseMessage;
import com.intellagric.pojo.AgriecomNewsType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @Autuor cky
 * @Date 2018/11/8 19:50
*/
@Controller
@RequestMapping("newsType")
public class NewsTypeController {

    @Autowired
    private NewsTypeService newsTypeService;

    /**
     * 根据id查找
     * @param id
     * @return
     */
    @RequestMapping("{id}")
    @ResponseBody
    public ResponseMessage selectOne(@PathVariable("id") String id){ return newsTypeService.selectNewsTypeById(id); }

    /**
     * 添加新闻信息
     * @param newsType
     * @return ResponseMessage
     */
    @RequestMapping("add")
    @ResponseBody
    public ResponseMessage addNews(AgriecomNewsType newsType){
        System.out.println("aaaa"+newsType.getRemarks());
        if(newsType!=null) {
            newsType.setId(UUID.randomUUID().toString().replace("-",""));
            if(newsType.getDelFlag()==null||newsType.getDelFlag().equals(""))
                newsType.setDelFlag("0");
            newsType.setCreateDate(new Date(System.currentTimeMillis()));
            newsType.setUpdateDate(new Date(System.currentTimeMillis()));
            return newsTypeService.insertNewsType(newsType);
        }
        return ResponseMessage.fail("插入数据为空");
    }

    /**
     * 删除新闻类别
     * @Param id
     * @return ResponseMessage
     */
    @RequestMapping("delete")
    @ResponseBody
    public ResponseMessage delete(String id) {
        return newsTypeService.deleteNewsType(id);
    }

    /**
     * 批量删除新闻类别
     * @Param id
     * @return ResponseMessage
     */
    @RequestMapping("deleteAll")
    @ResponseBody
    public ResponseMessage deleteAll(@RequestParam(name = "ids[]") String[] ids) {
        for(String id:ids){
            ResponseMessage result=newsTypeService.deleteNewsType(id);
            if(result.getCode()==100)
                return ResponseMessage.fail("删除异常");
        }

        return ResponseMessage.success();
    }


    /**
     * 编辑修改新闻种类信息
     * @Param newsType
     * @return ResponseMessage
     */
    @RequestMapping(value="edit")
    @ResponseBody
    public ResponseMessage edit(AgriecomNewsType newsType)  {


        if(newsType.getId()==null||newsType.getId().equals("")) {
            newsType.setId(UUID.randomUUID().toString());
            this.addNews(newsType);
        }
        try {
            newsType.setName(new String(newsType.getName().getBytes("iso-8859-1"), "UTF-8"));
            newsType.setRemarks(new String(newsType.getRemarks().getBytes("iso-8859-1"), "UTF-8"));
        }catch(UnsupportedEncodingException e){
            e.printStackTrace();
        }
        return newsTypeService.editNewsType(newsType);
    }

    /**
     * 分页查询(restful)
     * @param page
     * @param rows
     * @return
     */
    @RequestMapping("list/{page}&{rows}")
    @ResponseBody
    public LayuiDataGridResult query(@PathVariable("page") int page, @PathVariable("rows") int rows){
        return newsTypeService.getList(page,rows);
    }
    /**
     * 分页查询
     * @param page
     * @param rows
     * @return
     */
    @RequestMapping("list")
    @ResponseBody
    public LayuiDataGridResult queryPage(@RequestParam(name="page",defaultValue = "1") int page,@RequestParam(name = "rows",defaultValue = "20") int rows){
        return newsTypeService.getList(page,rows);
    }
    /**
     * 查询所有类型
     * @return
     */
    @RequestMapping("all")
    @ResponseBody
    public LayuiDataGridResult queryAll(){

        List<AgriecomNewsType> result=newsTypeService.getAll();
        return LayuiDataGridResult.success().add(result,result.size());
    }
    /**
     * 降序查询所有
     * @return
     */
    @RequestMapping("asc")
    @ResponseBody
    public LayuiDataGridResult queryAllDesc(){
        LayuiDataGridResult lay=new LayuiDataGridResult();
        List<AgriecomNewsType> list=newsTypeService.getAll();
        if(list==null)
            return LayuiDataGridResult.fail();
        lay.add(list,list.size());
        lay.setMsg("查询成功");
        return lay;
    }

    /**
     * 返回多级菜单树结构
     * @return
     */
    @RequestMapping("tree")
    @ResponseBody
    public LayuiDataGridResult getTree(){ return newsTypeService.getTreeList();}


    /**
     * 返回下拉选项多级
     * @return
     */
    @RequestMapping("selectTree")
    @ResponseBody
    public List<AgriecomNewsType> getSelectTree(){ return newsTypeService.getTreeList().getData();}



}
